package com.mvd.app.ac2020;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.Response;
import com.lzy.okgo.request.base.Request;
import com.mvd.app.R;
import com.mvd.app.activity.BaseActivity;
import com.mvd.app.activity.NewCarDetailsActivity;
import com.mvd.app.adapter.DiaoDuCarsListAdapter;
import com.mvd.app.application.BaseApplication;
import com.mvd.app.commons.CommonUtils;
import com.mvd.app.commons.Constants;
import com.mvd.app.commons.JsonUtil;
import com.mvd.app.commons.SharedPreferencesUtils;
import com.mvd.app.commons.UtilsLog;
import com.mvd.app.model.NewCarsModel;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * @author Administrator
 * @version V1.0
 * @Title: DiaoDuActivity
 * @Package com.mvd.app.ac2020
 * @Description: 调度---上线
 * @date 2020-08-19 9:00
 **/

public class DiaoDuActivity extends BaseActivity {

    @BindView(R.id.ac_diaodu_new_cars)
    ListView acDiaoduNewCars;
    @BindView(R.id.ac_diaodu_cars_fab)
    Button acDiaoduCarsFab;
    @BindView(R.id.ac_diaodu_btn_shangxian)
    Button acDiaoduBtnShangxian;
    @BindView(R.id.ac_diaodu_line)
    TextView acDiaoduLine;
    @BindView(R.id.ac_diaodu_ll)
    LinearLayout acDiaoduLl;
    private int CHECK_MODE = 0;
    private String Login_name = "", login_IDNumber;//当前登录的检测员的名字
    public DiaoDuActivity instances = null;
    //查询出来的列表
    private List<NewCarsModel> carsInforModelList = new ArrayList<NewCarsModel>();
    private DiaoDuCarsListAdapter diaoduAdapter;
    private String zhoushu = "0";//轴数
    private List<NewCarsModel> newCarsModelArrayList_intent = new ArrayList<NewCarsModel>();//传递数据用的
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_diao_du);
        ButterKnife.bind(this);
        setBackBtn();
        setTopTitle("调度上线");
//        setRightTxt("刷新", new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                getCarsList();
//            }
//        });
        instances = this;
        Login_name = getIntent().getExtras().getString("login_UserID", "");
        login_IDNumber = getIntent().getExtras().getString("login_IDNumber", "");
        CHECK_MODE = BaseApplication.JIANCE_MODE;
    }

    @Override
    protected void onResume() {
        super.onResume();
        getCarsList();
        zhoushu = "0";
        newCarsModelArrayList_intent = new ArrayList<NewCarsModel>();//传递数据用的
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        OkGo.getInstance().cancelTag(this);
        instances = null;
    }

    //获取列表
    private void getCarsList() {
//        if (carsInforModelList != null) {
//            carsInforModelList.clear();
//        }
        UtilsLog.e(instances.getClass().getName() + "getCarsList---url==" + SharedPreferencesUtils.getIP(instances) + CARS_LIST);
        OkGo.<String>get(SharedPreferencesUtils.getIP(instances) + CARS_LIST).tag(instances)
                .execute(new StringCallback() {
                    @Override
                    public void onStart(Request<String, ? extends Request> request) {
                        super.onStart(request);
                        CommonUtils.showLoadingDialog(instances, "加载中...");
                    }

                    @Override
                    public void onSuccess(Response<String> response) {
                        CommonUtils.hideLoadingDialog(instances);
                        String result = response.body();
//                        UtilsLog.e("getDataUsersList-result==" + result);
                        String newResult = result.substring(1, result.length() - 1).replaceAll("\\\\", "");
                        UtilsLog.e(instances.getClass().getName() + "getDataUsersList-newResult==" + newResult);
                        if (!TextUtils.isEmpty(newResult) && !"[[]]".equals(newResult) && !"[{}]".equals
                                (newResult) && !"[]".equals(result)) {
                            carsInforModelList = new ArrayList<NewCarsModel>();
                            carsInforModelList = JsonUtil.stringToArray(newResult,
                                    NewCarsModel[].class);
                            if (carsInforModelList != null && carsInforModelList.size() > 0) {
                                acDiaoduNewCars.setVisibility(View.VISIBLE);
                                diaoduAdapter = new DiaoDuCarsListAdapter(instances,
                                        carsInforModelList);
                                acDiaoduNewCars.setAdapter(diaoduAdapter);
                            } else {
                                acDiaoduNewCars.setVisibility(View.GONE);
//                                carsInforListAdapter.notifyDataSetChanged();
                                UtilsLog.e(instances.getClass().getName() + "getCarsList-result=22=" + "2222没有数据");
                            }
                        } else {
                            UtilsLog.e(instances.getClass().getName() + "getCarsList-result==" + "没有数据");
                        }
                    }

                    @Override
                    public void onError(Response<String> response) {
                        super.onError(response);
                        CommonUtils.hideLoadingDialog(instances);
                        UtilsLog.e(instances.getClass().getName() + "getCarsList-onError==" + response.body());
                        Toast.makeText(instances, getResources().getString(R.string.services_error), Toast
                                .LENGTH_LONG).show();
                    }
                });
    }

    @OnClick({R.id.ac_diaodu_cars_fab, R.id.ac_diaodu_btn_shangxian
    , R.id.ac_diaodu_ll})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.ac_diaodu_cars_fab:
                //刷新
                getCarsList();
                break;
            case R.id.ac_diaodu_btn_shangxian:
                //上线
                int maxAccount = 0;
                for (int i = 0; i < DiaoDuCarsListAdapter.sSelected.size(); i++) {
                    if (DiaoDuCarsListAdapter.sSelected.get(i)) {
                        maxAccount = maxAccount + 1;
                    }
                }

                UtilsLog.e("选择的行数：maxAccount==" + maxAccount);
                if (maxAccount > 2) {
                    Toast.makeText(instances, "选择数目不能大于两辆", Toast.LENGTH_LONG).show();
                } else if (maxAccount == 2) {
                    //上线

                    for (int i = 0; i < DiaoDuCarsListAdapter.sSelected.size(); i++) {
                        if (DiaoDuCarsListAdapter.sSelected.get(i)) {
                            newCarsModelArrayList_intent.add(carsInforModelList.get(i));
                        }
                    }
                    if (newCarsModelArrayList_intent != null && newCarsModelArrayList_intent.size() > 0) {
                        Intent intent = new Intent(instances, NewCarDetailsActivity.class);
                        intent.putExtra("new_car_model", (Serializable)newCarsModelArrayList_intent);
                        intent.putExtra("login_UserID", Login_name);
                        intent.putExtra("login_IDNumber", login_IDNumber);
                        intent.putExtra("zhoushu", zhoushu);
                        startActivity(intent);
                    } else {
                        UtilsLog.e("maxAccount == 2---没有数据");
                    }
                } else if (maxAccount == 1) {
                    int index = 0;
                    NewCarsModel model = null;
                    for (int i = 0; i < DiaoDuCarsListAdapter.sSelected.size(); i++) {
                        if (DiaoDuCarsListAdapter.sSelected.get(i)) {
                            index = i;
                        }
                    }
                    model = carsInforModelList.get(index);
                    newCarsModelArrayList_intent.add(model);
                    if (model.getPlateType().equals("15") || (model.getType().substring(0, 1))
                            .equalsIgnoreCase("Q")) {
                        dialogList(newCarsModelArrayList_intent); // 轴数列表
                    } else {
                        //上线
                        Intent intent = new Intent(instances, NewCarDetailsActivity.class);
                        intent.putExtra("new_car_model", (Serializable)newCarsModelArrayList_intent);
                        intent.putExtra("login_UserID", Login_name);
                        intent.putExtra("login_IDNumber", login_IDNumber);
                        intent.putExtra("zhoushu", zhoushu);
                        startActivity(intent);
                    }
                } else if (maxAccount == 0) {
                    Toast.makeText(instances, "请先选择车辆", Toast.LENGTH_LONG).show();
                    UtilsLog.e("列表没有数据");
                }
                break;
            case R.id.ac_diaodu_ll:
                //线号
                break;
        }
    }

    private void dialogList(final List<NewCarsModel> model) {
        final String items[] = Constants.ZHOUSHOU_LIST;

        AlertDialog.Builder builder = new AlertDialog.Builder(instances, 3);
        builder.setTitle("请选择轴数");
        // builder.setMessage("是否确认退出?"); //设置内容
//        builder.setIcon(R.mipmap.ic_launcher);
        // 设置列表显示，注意设置了列表显示就不要设置builder.setMessage()了，否则列表不起作用。
        builder.setItems(items, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                zhoushu = items[which];
                //上线
                Intent intent = new Intent(instances, NewCarDetailsActivity.class);
                intent.putExtra("new_car_model", (Serializable)model);
                intent.putExtra("login_UserID", Login_name);
                intent.putExtra("login_IDNumber", login_IDNumber);
                intent.putExtra("zhoushu", zhoushu);
                startActivity(intent);

//                dialog.dismiss();
//                Toast.makeText(instances, items[which],
//                        Toast.LENGTH_SHORT).show();

            }
        });
        builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
//                Toast.makeText(instances, "确定", Toast.LENGTH_SHORT)
//                        .show();
            }
        });
        builder.create().show();
    }
}
